package app.services.impl;

import com.digitalpersona.onetouch.DPFPFingerIndex;
import com.thoughtworks.xstream.core.util.Base64Encoder;

import app.models.BiometriaTemplate;
import app.repositories.BiometriaTemplateRepository;
import app.repositories.Repository;
import app.services.BiometriaTemplateService;
import br.com.caelum.vraptor.ioc.Component;

@Component
public class BiometriaTemplateServiceImpl extends GenericServiceImpl<BiometriaTemplate, Long> implements BiometriaTemplateService{

	private BiometriaTemplateRepository repository;
	
	public BiometriaTemplateServiceImpl(BiometriaTemplateRepository repository){
		this.repository = repository;
		super.setGenericRepository((Repository<BiometriaTemplate, Long>) repository);
	}
	
	public BiometriaTemplate findBiometriaTemplateByPacienteId(Integer id){
		return repository.findBiometriaTemplateByPacienteId(id);
	}
	
	public BiometriaTemplate findBiometriaTemplateByPacienteIdAndByFinger(Integer id, String finger){
		return repository.findBiometriaTemplateByPacienteIdAndByFinger(id,finger);
	}
	
	public byte[] decodeString(String encoded){
		
		Base64Encoder encoder = new Base64Encoder();
		byte[] decoded = encoder.decode(encoded);
		
		return decoded;
		
	}
	
	public BiometriaTemplate createTemplate(Integer id, String finger, byte[] decoded){
		
		BiometriaTemplate template = new BiometriaTemplate();
		
		template.setDedo(finger);
		template.setIdPaciente(id);
		template.setTemplate(decoded);
		
		return template;
	}
	
}
